package com.smartwasser.yunhtgl.apphtgl.asps;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.activity.ImageLookActivity;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.bean.BaseBean;
import com.smartwasser.yunhtgl.apphtgl.bean.FlMessageBean;
import com.smartwasser.yunhtgl.apphtgl.bean.SPGreat1Bean;
import com.smartwasser.yunhtgl.apphtgl.function.OfficeReader;
import com.smartwasser.yunhtgl.apphtgl.photo.ImagePagerActivity;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPGreat1Presenter;
import com.smartwasser.yunhtgl.apphtgl.presenter.SPGreat1PresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.view.SPGreat1View;
import com.smartwasser.yunhtgl.apphtgl.widget.QianzigaizhangView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dou361.dialogui.DialogUIUtils.dismiss;
import static com.smartwasser.yunhtgl.apphtgl.network.Retrofits.base;
import static com.smartwasser.yunhtgl.apphtgl.utils.BaseUtils.isBCVisible;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.getStringData;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getGreat;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getPath;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getProcurementMode;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getQicaoType;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getSZType;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getTime;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getType;

/**
 * 待完成合同详情接口
 * 个人已处理
 * 重大合同详情
 */
public class SPGreat2Activity extends BaseActivity implements SPGreat1View {
    private SPGreat1Presenter sp1GreatPresenter = new SPGreat1PresenterImpl(this);
    private SPGreat1Bean type1Sp1GreatBean;
    private Dialog loadingHorizontal;
    private String id;
    @BindView(R.id.tv_toolbar)
    TextView tv_toolbar;
    @BindView(R.id.sp1_great_scroll)
    ScrollView sp1_great_scroll;

    @BindView(R.id.sp1_great_code)
    TextView sp1_great_code;//合同编号
    @BindView(R.id.sp1_great_procurementMode)
    TextView sp1_great_procurementMode;//集采方式
    @BindView(R.id.sp1_great_tzs)
    TextView sp1_great_tzs;//中标通知书
    @BindView(R.id.sp1_great_lxd)
    TextView sp1_great_lxd;//工作联系单
    @BindView(R.id.sp1_great_qt)
    TextView sp1_great_qt;//其他资料
    @BindView(R.id.sp1_great_xz)
    TextView sp1_great_xz;//合同性质
    @BindView(R.id.sp1_great_name)
    TextView sp1_great_name;//合同名称
    @BindView(R.id.sp1_great_jia)
    TextView sp1_great_jia;//合同甲方
    @BindView(R.id.sp1_great_yi)
    TextView sp1_great_yi;//合同乙方
    @BindView(R.id.sp1_great_qtf)
    TextView sp1_great_qtf;//合同其他方
    @BindView(R.id.sp1_great_main)
    TextView sp1_great_main;//合同主要内容
    @BindView(R.id.sp1_great_bd)
    TextView sp1_great_bd;//合同标的
    @BindView(R.id.sp1_great_price)
    TextView sp1_great_price;//合同金额
    @BindView(R.id.sp1_great_start_time)
    TextView sp1_great_start_time;//合同执行日期
    @BindView(R.id.sp1_great_fen)
    TextView sp1_great_fen;//合同份数
    @BindView(R.id.sp1_great_sq)
    TextView sp1_great_sq;//合同申请部室
    @BindView(R.id.sp1_great_zgy)
    TextView sp1_great_zgy;//合同专管员
    @BindView(R.id.sp1_great_creat_time)
    TextView sp1_great_creat_time;//合同申请时间
    @BindView(R.id.sp1_great_fzr)
    TextView sp1_great_fzr;//部室负责人
    @BindView(R.id.sp1_great_qc)
    TextView sp1_great_qc;//起草
    @BindView(R.id.sp1_great_zw)
    TextView sp1_great_zw;//正文
    @BindView(R.id.sp1_great_yj)
    TextView sp1_great_yj;//法律意见书
    @BindView(R.id.sp1_great_sz)
    TextView sp1_great_sz;//收支情况
    @BindView(R.id.sp1_great_lx)
    TextView sp1_great_lx;//合同类型
    @BindView(R.id.sp1_great_qz)
    TextView sp1_great_qz;//签字盖章

    @BindView(R.id.great_item_yy_time)
    TextView great_item_yy_time;//运营
    @BindView(R.id.great_item_yy_main)
    TextView great_item_yy_main;
    @BindView(R.id.great_item_yy_qz)
    TextView great_item_yy_qz;
    @BindView(R.id.great_item_fw_time)
    TextView great_item_fw_time;//法务
    @BindView(R.id.great_item_fw_main)
    TextView great_item_fw_main;
    @BindView(R.id.great_item_fw_qz)
    TextView great_item_fw_qz;
    @BindView(R.id.great_item_fg_time)
    TextView great_item_fg_time;//分管
    @BindView(R.id.great_item_fg_main)
    TextView great_item_fg_main;
    @BindView(R.id.great_item_fg_qz)
    TextView great_item_fg_qz;
    @BindView(R.id.great_item_zjl_time)
    TextView great_item_zjl_time;//分管
    @BindView(R.id.great_item_zjl_main)
    TextView great_item_zjl_main;
    @BindView(R.id.great_item_zjl_qz)
    TextView great_item_zjl_qz;
    @BindView(R.id.sp1_great_fl_all)
    LinearLayout sp1_great_fl_all;//补充信息可不可见
    private ArrayList<String> imageUrls = new ArrayList<>();

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_sp_great2);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        tv_toolbar.setText("重大合同详情");
        sp1_great_scroll.setVisibility(View.INVISIBLE);
        id = getIntent().getStringExtra("id");
    }

    @Override
    public void initData() {
        loadingHorizontal = showLoadingHorizontal(this, "加载中...").show();
        try {
            sp1GreatPresenter.getType1Sp1Great(getStringData(this, "JSESSIONID", ""), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.back, R.id.sp1_great_tzs_look, R.id.sp1_great_lxd_look, R.id.sp1_great_zw_all})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sp1_great_tzs_look:
                if (null != type1Sp1GreatBean.getBuContractBaseInfo()
                        && type1Sp1GreatBean.getBuContractBaseInfo().getTongzhishuList().size() > 0) {
                    String path = type1Sp1GreatBean.getBuContractBaseInfo().getTongzhishuList().get(0).getPath();
                    if (!path.isEmpty() && path.length() > 0) {
                        startActivity(OfficeReader.class, "url", path);
                    } else {
                        showResult("暂无数据");
                    }
                } else {
                    showResult("暂无数据");
                }
                break;
            case R.id.sp1_great_lxd_look:
                if (null != type1Sp1GreatBean.getBuContractBaseInfo()
                        && type1Sp1GreatBean.getBuContractBaseInfo().getLianxidanList().size() > 0) {
                    String path = type1Sp1GreatBean.getBuContractBaseInfo().getLianxidanList().get(0).getPath();
                    if (!path.isEmpty() && path.length() > 0) {
                        startActivity(OfficeReader.class, "url", path);
                    } else {
                        showResult("暂无数据");
                    }
                } else {
                    showResult("暂无数据");
                }
                break;
            case R.id.sp1_great_zw_all:
                if (null != type1Sp1GreatBean.getBuContractBaseInfo()) {
                    String path = type1Sp1GreatBean.getBuContractBaseInfo().getPath();
                    if (!path.isEmpty() && path.length() > 0) {
                        startActivity(OfficeReader.class, "url", path);
                    } else {
                        showResult("暂无数据");
                    }
                } else {
                    showResult("暂无数据");
                }
                break;
        }
    }

    @Override
    public void success(SPGreat1Bean tb) {
        if (null != tb && null != tb.getBuContractBaseInfo()) {
            this.type1Sp1GreatBean = tb;
            sp1_great_code.setText(tb.getBuContractBaseInfo().getCode());
            sp1_great_xz.setText(getGreat(tb.getBuContractBaseInfo().getContractNature()));
            sp1_great_zw.setText(getPath(tb.getBuContractBaseInfo().getPath()));
            sp1_great_lx.setText(getType(tb.getBuContractBaseInfo().getType()));
            sp1_great_qc.setText(getQicaoType(tb.getBuContractBaseInfo().getQicaoType()));
            sp1_great_sz.setText(getSZType(tb.getBuContractBaseInfo().getShouzhiType()));
            if (tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getUnitPriceOpinion().length() > 0) {
                String money = tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getUnitPriceOpinion() + "\n" +
                        tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractMoney() + "元";
                sp1_great_price.setText(money);
            } else {
                String money = tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractMoney() + "元";
                sp1_great_price.setText(money);
            }
            sp1_great_name.setText(tb.getBuContractBaseInfo().getName());
            sp1_great_procurementMode.setText(getProcurementMode(tb.getBuContractBaseInfo().getProcurementMode()));
            if (null != tb.getBuContractBaseInfo().getTongzhishuList() && tb.getBuContractBaseInfo().getTongzhishuList().size() > 0) {
                sp1_great_tzs.setText(tb.getBuContractBaseInfo().getTongzhishuList().get(0).getName());
            }
            if (null != tb.getBuContractBaseInfo().getLianxidanList() && tb.getBuContractBaseInfo().getLianxidanList().size() > 0) {
                sp1_great_lxd.setText(tb.getBuContractBaseInfo().getLianxidanList().get(0).getName());
            }

            if (null != tb.getBuContractBaseInfo().getBuMajorContractSignInfoList() && tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().size() > 0) {
                sp1_great_jia.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getJiaFangName());
                sp1_great_yi.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getYiFangName());
                sp1_great_qtf.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getOtherFangName());
                sp1_great_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractMainContent());
                sp1_great_bd.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractBiaodi());
                String time = getTime(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractExcuteTimeType(),
                        tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractExcuteTimeStart(),
                        tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractExcuteTimeEnd(),
                        tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractExcuteTime());
                sp1_great_start_time.setText(time);
                sp1_great_fen.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractCount() + "份");
                sp1_great_sq.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractApplyDept().getName());
                sp1_great_zgy.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractOfficer().getName());
                sp1_great_creat_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractApplyDate());
                sp1_great_fzr.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getContractDwOfficer().getName());

                great_item_yy_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveCheckDate());
                great_item_yy_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveUpdateOpinion());
                great_item_yy_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveName().getName());
                great_item_fw_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptCheckDate());
                great_item_fw_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptUpdateOpinion());
                great_item_fw_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptOfficerName().getName());
                great_item_fg_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeUpdateDate());
                great_item_fg_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeOpinion());
                great_item_fg_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeLeader().getName());
                great_item_zjl_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupGeneralManagerUpdateDate());
                great_item_zjl_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupGeneralManagerOpinion());
                great_item_zjl_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupGeneralManagerName().getName());

                great_item_yy_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveCheckDate());
                great_item_yy_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveUpdateOpinion());
                great_item_yy_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getBusinessExecutiveName().getName());
                great_item_fw_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptCheckDate());
                great_item_fw_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptUpdateOpinion());
                great_item_fw_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getLegalDeptOfficerName().getName());
                great_item_fg_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeUpdateDate());
                great_item_fg_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeOpinion());
                great_item_fg_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupChargeLeader().getName());
                great_item_zjl_time.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupGeneralManagerUpdateDate());
                great_item_zjl_main.setText(tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupGeneralManagerOpinion());
                great_item_zjl_qz.setText("负责人签字 : " + tb.getBuContractBaseInfo().getBuMajorContractSignInfoList().get(0).getGroupGeneralManagerName().getName());
            }
                sp1_great_fl_all.setVisibility(View.VISIBLE);
                String qzht = tb.getBuContractBaseInfo().getQianziContract();
                if (!qzht.isEmpty() && qzht.length() > 0) {
                    final String[] qiht=qzht.split("\\|");
                    for(int i=1;i<qiht.length;i++){
                        imageUrls.add(base+qiht[i]);
                        QianzigaizhangView qianzigaizhangView=new QianzigaizhangView(SPGreat2Activity.this,null);
                        String[] qzgz_array = qiht[i].split("/");
                        qianzigaizhangView.setText(qzgz_array[qzgz_array.length-1]);
                        if (i==1)
                            qianzigaizhangView.setVisibilityTitle();
                        sp1_great_fl_all.addView(qianzigaizhangView);
                        final int finalI = i;
                        qianzigaizhangView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (null != type1Sp1GreatBean.getBuContractBaseInfo()) {
                                    String path = qiht[finalI];
                                    if (!path.isEmpty() && path.length() > 0) {
                                        if(path.contains(".JPG")||path.contains(".PNG")||path.contains(".jpg")||path.contains(".png")){
                                            imageBrower(finalI-1);
//                                            startActivity(ImageLookActivity.class, "url", path);
                                        }else{
                                            startActivity(OfficeReader.class, "url", path);
                                        }
                                    } else {
                                        showResult("暂无数据");
                                    }
                                } else {
                                    showResult("暂无数据");
                                }
                            }
                        });
                    }
                }
        }
        sp1_great_scroll.setVisibility(View.VISIBLE);
        dismiss(loadingHorizontal);
    }

    private void imageBrower(int position) {
        Intent intent = new Intent(this, ImagePagerActivity.class);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, imageUrls);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        startActivity(intent);
    }
    @Override
    public void successGetFlMessage(FlMessageBean flMessageBean) {

    }

    @Override
    public void successCommitSH(BaseBean baseBean) {

    }

    @Override
    public void error() {
        dismiss(loadingHorizontal);
        showError();
    }
}